package com.lange.autocode.sysManager.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.core.bean.ResultT;
import org.springframework.stereotype.Service;

@Service
public class OrganServ {
    private final Dao dao;

    public OrganServ(Dao dao) {
        this.dao = dao;
    }


    /**
     * 更新组织机构信息
     * @param fieldName
     * @param data
     * @return
     */
    public ResultT update(String fieldName, JSONObject data){
        switch(fieldName){
            case "label":
                dao.executeUpdate("update sys_organ set label=? where id=?",data.getString("label"),data.getString("id"));
                break; // 添加 break 语句，防止 fall-through
            case "orgType":
                dao.executeUpdate("update sys_organ set org_type=? where id=?",data.getString("orgType"),data.getString("id"));
                break;
            case "contact":
                dao.executeUpdate("update sys_organ set contact=? where id=?",data.getString("contact"),data.getString("id"));
                break;
            case "tel":
                dao.executeUpdate("update sys_organ set tel=? where id=?",data.getString("tel"),data.getString("id"));
                break;
            case "fax":
                dao.executeUpdate("update sys_organ set fax=? where id=?",data.getString("fax"),data.getString("id"));
                break;
            case "email":
                dao.executeUpdate("update sys_organ set email=? where id=?",data.getString("email"),data.getString("id"));
                break;
            case "address":
                dao.executeUpdate("update sys_organ set address=? where id=?",data.getString("address"),data.getString("id"));
                break;
            case "longitude":
                dao.executeUpdate("update sys_organ set longitude=? where id=?",data.getString("longitude"),data.getString("id"));
                break;
            case "latitude":
                dao.executeUpdate("update sys_organ set latitude=? where id=?",data.getString("latitude"),data.getString("id"));
                break;
            case "remark":
                dao.executeUpdate("update sys_organ set remark=? where id=?",data.getString("remark"),data.getString("id"));
                break;
            case "area":
                JSONArray area = data.getJSONArray("area");
                if(area!=null&&area.size()==3){
                    dao.executeUpdate("update sys_organ set area=?,province=?,city=?,county=? where id=?",area.toJSONString(),area.getString(0),area.getString(1),area.getString(2),data.getString("id"));
                }else{
                    dao.executeUpdate("update sys_organ set area=null,province=null,city=null,county=null where id=?",data.getString("id"));
                }
                break;
        }
        return ResultT.success(data);
    }
}
